package cn.czh.service.impl;

import cn.czh.dao.TeacherDao;
import cn.czh.entity.Teacher;
import cn.czh.service.TeacherService;
import cn.czh.utils.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author czh
 * @since 2021-05-31
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherDao teacherDao;

    @Override
    public int addTeacher(Teacher teacher) {
        int i = teacherDao.insert(teacher);
        return i;
    }

    @Override
    public int deleteTeachers(Integer[] ids) {
        int i = teacherDao.deleteBatchIds(Arrays.asList(ids));
        return i;
    }

    @Override
    public int updateTeacherById(Teacher teacher) {
        int i = teacherDao.updateById(teacher);
        return i;
    }

    @Override
    public List<Teacher> findAll(Teacher teacher) {
        System.out.println(teacher);
        EntityWrapper<Teacher> wrapper =new EntityWrapper<>();
        if(!teacher.getUsername().equals(""))
        {
            wrapper.like("username",teacher.getUsername());
        }
        if(!teacher.getRealname().equals(""))
        {
            wrapper.like("realname",teacher.getRealname());
        }
        if(!teacher.getPhone().equals(""))
        {
            wrapper.like("phone",teacher.getPhone());
        }
        if(StringUtils.isNotNull(teacher.getSex()))
        {
            wrapper.eq("sex",teacher.getSex());
        }
        if(!teacher.getTitle().equals(""))
        {
            wrapper.like("title",teacher.getTitle());
        }
        List<Teacher> all = teacherDao.selectList(wrapper);
        return all;
    }

    /**
     *
     * @param teacher
     * @return 0 用户不存在 1 密码错误 2成功
     */
    @Override
    public int login(Teacher teacher) {
        EntityWrapper<Teacher> entityWrapper =new EntityWrapper<>();
        entityWrapper.eq("username",teacher.getUsername());
        List<Teacher> teachers = teacherDao.selectList(entityWrapper);
        if(teachers.size()==1)
        {
            Teacher byUsername = teachers.get(0);
            if(StringUtils.isNotNull(byUsername))
        {
            if(byUsername.getPassword().equals(teacher.getPassword()))
            {
                return 2;
            }
            return 1;
        }
        }
        return 0;
    }

    @Override
    public Teacher findTeacherById(Integer id) {
        Teacher byId = teacherDao.selectById(id);
        return byId;
    }

    @Override
    public Integer getTeacherId(String username) {
        EntityWrapper<Teacher> entityWrapper =new EntityWrapper<>();
        entityWrapper.eq("username",username);
        List<Teacher> teachers = teacherDao.selectList(entityWrapper);
        Integer id = teachers.get(0).getId();
        return id;
    }

    @Override
    public Integer resetPassword(Integer id, String password) {
        Teacher teacherById = findTeacherById(id);
        teacherById.setPassword(password);
        int i = updateTeacherById(teacherById);
        return i;
    }
}
